package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.OrderReturnApplyApi;
import com.yoda.mall.model.entity.oms.CompanyAddress;
import com.yoda.mall.model.entity.oms.OrderReturnApply;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ReturnApplyVo;
import com.yoda.mall.mysql.mapper.CompanyAddressMapper;
import com.yoda.mall.mysql.mapper.OrderReturnApplyMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 12月 18日 21:10
 */
@DubboService
public class OrderReturnApplyApiImpl implements OrderReturnApplyApi {

    @Resource
    private OrderReturnApplyMapper orderReturnApplyMapper;


    @Resource
    private CompanyAddressMapper companyAddressMapper;

    @Override
    public PageResult<OrderReturnApply> getReturnApplyByPage(Integer page, Integer pageSize, OrderReturnApply returnApply) {
        Page<OrderReturnApply> returnApplyPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<OrderReturnApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(returnApply.getId()),OrderReturnApply::getId,returnApply.getId());
        wrapper.eq(Objects.nonNull(returnApply.getStatus()),OrderReturnApply::getStatus,returnApply.getStatus());
        wrapper.ge(Objects.nonNull(returnApply.getCreateTime()),OrderReturnApply::getCreateTime,returnApply.getCreateTime());
        wrapper.le(Objects.nonNull(returnApply.getHandleTime()),OrderReturnApply::getHandleTime,returnApply.getHandleTime());
        wrapper.like(StringUtils.isNotEmpty(returnApply.getHandleMan()),OrderReturnApply::getHandleMan,returnApply.getHandleMan());
        orderReturnApplyMapper.selectPage(returnApplyPage,wrapper);
        return new PageResult<>(page,pageSize,(int)returnApplyPage.getTotal(),returnApplyPage.getRecords());
    }

    @Override
    public Integer deleteReturnByIds(List<Long> ids) {
        LambdaQueryWrapper<OrderReturnApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderReturnApply::getStatus,3);
        wrapper.in(OrderReturnApply::getId,ids);
        return orderReturnApplyMapper.delete(wrapper);
    }

    @Override
    public ReturnApplyVo getReturnApplyById(Long id) {
        ReturnApplyVo vo = new ReturnApplyVo();
        OrderReturnApply orderReturnApply = orderReturnApplyMapper.selectById(id);
        BeanUtils.copyProperties(orderReturnApply,vo);
        Long companyAddressId = orderReturnApply.getCompanyAddressId();
        if (Objects.isNull(companyAddressId)){
                return vo;
        }
        CompanyAddress companyAddress = companyAddressMapper.selectById(companyAddressId);
        vo.setCompanyAddress(companyAddress);
        return vo;
    }

    @Override
    public Integer updateStatus(OrderReturnApply returnApply) {
       return orderReturnApplyMapper.updateById(returnApply);
    }
}
